package com.jwell.panprovider.ui.fragment.transportplaneinfo


import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.jwell.cargoapp.ui.base.MVPBaseFragment

import com.jwell.panprovider.R
import com.jwell.panprovider.contract.TransportPlaneInfoContract
import com.jwell.panprovider.entity.AppContratcListDto
import com.jwell.panprovider.entity.AppFuelPlanDtlDto
import com.jwell.panprovider.presenter.TransportPlaneInfoPresenter
import com.jwell.panprovider.tools.StringUtils
import com.jwell.panprovider.ui.activity.ProviderRegisterActivity
import com.jwell.panprovider.ui.adapter.TransportInfoContractAdapter
import com.jwell.panprovider.ui.dialog.TransportPlaneInfoDialog
import kotlinx.android.synthetic.main.fragment_transpor_plane_info.*


/**
 * A simple [Fragment] subclass.
 * 计划信息
 */
class TransportPlaneInfoFragment : MVPBaseFragment<TransportPlaneInfoPresenter>(), TransportPlaneInfoContract.View {

    override fun createPresenter(): TransportPlaneInfoPresenter? {
        return TransportPlaneInfoPresenter()
    }

    private var listData = ArrayList<AppContratcListDto>()

    private var adapter = TransportInfoContractAdapter(listData)

    //是否点击列表预登记Button
    private var isOnclickRegisterBtn = false
    private var send_Com: String? = null
    private var receipt_Com: String? = null
    private var fuelPlanId: Int = 0
    private var data: AppFuelPlanDtlDto? = null
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater!!.inflate(R.layout.fragment_transpor_plane_info, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        showProgress("", true)
        activity?.intent?.let {
            fuelPlanId = it.getIntExtra("id", 0)
            presenter?.getData(fuelPlanId)
        }
        //修改供应车数
        text_amend.setOnClickListener {
            setText_amend_carNum(0)
        }
        adapter.setRegisterOnclickListener(object : TransportInfoContractAdapter.RegisterOnclickListener {
            override fun onClick(position: Int) {
                if ("0车" == text_carNum.text.toString()) {
                    isOnclickRegisterBtn = true
                    setText_amend_carNum(position)
                } else {
                    val intent = Intent(context, ProviderRegisterActivity::class.java)
                    intent.putExtra("fuelDetailId", listData[position].fuelDetailId)
                    intent.putExtra("mtrlOrderNo", listData[position].mtrlOrderNo)
                    startActivity(intent)
                }
            }
        })
    }

    //修改供应车数
    private fun setText_amend_carNum(position: Int) {
        TransportPlaneInfoDialog.Builder(context)
                .setOnEnsureClickListener(object : TransportPlaneInfoDialog.EnSureOnClickListener {
                    override fun onClick(carNum: Int) {
                        text_carNum.text = carNum.toString() + "车"
                        presenter?.appUpdateEstimate(fuelPlanId, carNum)
//                        if (isOnclickRegisterBtn) {
//                            isOnclickRegisterBtn = false
//                            var intent = Intent(context, ProviderRegisterActivity::class.java)
//                            val bundle = Bundle()
//                            bundle.putSerializable("data", listData[position])
//                            intent.putExtras(bundle)
//                            intent.putExtra("fuelDetailId", listData[position].fuelDetailId)
//                            intent.putExtra("mtrlOrderNo", listData[position].mtrlOrderNo)
//                            intent.putExtra("send_Com", send_Com)
//                            intent.putExtra("receipt_Com", receipt_Com)
//                            startActivity(intent)
//                        }
                    }
                })
                .create().show()
    }

    override fun onResume() {
        super.onResume()
        activity?.intent?.let {
            presenter?.getData(it.getIntExtra("id", 0))
        }
    }

    override fun showData(data: AppFuelPlanDtlDto) {
//        data.contractList?.get(0)?.fuelDetailId?.let {
//            fuelPlanId = it
//        }
        this.data = data
        text_carNum.text = data.estimateCarCount.toString() + "车"
        text_jh_code.text = data.planCode ?: ""
        text_provide_time.text = StringUtils.getInstance()
                .setModal(StringUtils.TIME_TYPE_1)
                .regularization(data.planDate, "T", " ")
        text_sendCom.text = data.forwardingUnit ?: ""
        send_Com = data.forwardingUnit ?: ""
        text_receiptCom.text = data.receivingUnit ?: ""
        receipt_Com = data.receivingUnit ?: ""
        text_can_grab_num.text = data.snatchingCount.toString() + "车"
        text_appoint_num.text = data.appointCarCount.toString() + "车"
        text_can_provide_num.text = data.supplyCount.toString() + "车"
        text_have_provide_num.text = data.accumulativeSupply.toString() + "车"
        if (data.isUnlimited) {
            text_can_provide_num.text = "不限"
            text1.visibility = View.GONE
            text2.visibility = View.GONE
            text_appoint_num.visibility = View.GONE
            text_can_grab_num.visibility = View.GONE
        } else {
            text1.visibility = View.VISIBLE
            text2.visibility = View.VISIBLE
            text_appoint_num.visibility = View.VISIBLE
            text_can_grab_num.visibility = View.VISIBLE
        }
        listData.clear()
        listData.addAll(data.contractList)
        recyclerView.adapter = adapter
        when (data.fuelState) {
            2 -> {//供应中
                adapter.setBooleShowRegister(true)
            }
            3 -> {//供应结束
                adapter.setBooleShowRegister(false)
                text_amend.visibility = View.GONE
            }
        }
        recyclerView.layoutManager = LinearLayoutManager(context)
        adapter.setPlaneType(data.planType)
        if(data.fuelState==2&&data.isFixedPoint)// 2、供应中 3、供应结束
        {
            ll_info_time.visibility=View.VISIBLE
//            {供应时间-年月日}放号安排如下：{放号时间}放号{放号车数}车  2018-11-20T00:00:00
            var date:String=data.planDate
            if(date!=null)
            {
                date=data.planDate.split("T")[0]
            }
            var value=date+"放号安排如下:"+data.fixedPointStr
            tv_info_time.text=value
        }else
        {
            ll_info_time.visibility=View.GONE
        }
    }
}
